<template>
    <el-date-picker
            v-model="model"
            :type="type"
            @change="changed()"
            :placeholder="placeholder"
             size="small"
            :disabled="disabled"
            :stamp="stamp"
    ></el-date-picker>  
</template>
<script>
    export default {
        name: 'XDDatePicker',
        props: ['value', 'type', 'placeholder', 'disabled',"stamp"],  //type: date, dateTime
        data() {
            return {
                input: '',
                model: null,
            }
        },
        created() {
            this.model = this.value
        },
        methods: {
            changed() {
              
                if (this.type == 'date') {
                    this.dateChanged()
                }else{
                    this.dateTimeChanged(this.stamp) 
                }
                this.$emit('input', this.input)
                this.$emit('change', this.input)
            },
            // date转换
            dateChanged(){
                this.input = this.dateFilterData(this.model)
            },
            datastamp(e){
                let d = new Date(e).valueOf()
                return d
            },
            dateFilterData(input) {
                var d = new Date(input);
                var year = d.getFullYear();
                var month =
                    d.getMonth() < 9 ? "0" + (d.getMonth() + 1) : "" + (d.getMonth() + 1);
                var day = d.getDate() < 10 ? "0" + d.getDate() : "" + d.getDate();
                return year + "-" + month + "-" + day;
            },
            // dateTime转换
            dateTimeChanged() {
                if(this.stamp=='stamp'){
                   this.input =  this.datastamp(this.model)   
                }else{
                    this.input = this.dateFilter(this.model)
                }
                
            },
            dateFilter(input) {
                var d = new Date(input);
                var year = d.getFullYear();
                var month =
                    d.getMonth() < 9 ? "0" + (d.getMonth() + 1) : "" + (d.getMonth() + 1);
                var day = d.getDate() < 10 ? "0" + d.getDate() : "" + d.getDate();
                var hh = d.getHours() < 10 ? "0" + d.getHours() : "" + d.getHours();
                var mm = d.getMinutes() < 10 ? "0" + d.getMinutes() : "" + d.getMinutes();
                var ss = d.getSeconds() < 10 ? "0" + d.getSeconds() : "" + d.getSeconds();
                return year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;
            },
        },
        watch: {
            value: function () {
                this.model = this.value
            }
        }  
    }
</script>